﻿/// <reference path="login.js" />
var storage = window.localStorage;

$(document).ready(function() {

    gettouxiang = function () {
		$.ajax({
			type: 'get',
			url: 'Pages/handle/getImage.ashx',
			async: true,
			success: function (data) {
			    if (data.result != "") {
			        storage.setItem("newhead", data.result);
					$("#touxiangicon").attr('src', data.result);					
					if($("#uploadImg")) {
						$("#uploadImg").attr('src', data.result);
					}
				}
			},
			error: function() {
				$.tooltip('服务器异常');
			},
			dataType: "json"
		})
	}

		$.ajax({
			type: 'GET',
			url: '../../Backstage/Pages/handle/isLogin.ashx',
			async: false,
			success: function (data) {
			    if (data.result == 105) {
					$('#login_li').css("display", "none");
					$('#register_li').css("display", "none");
					$('#username_li').css("display", "");
					$('#loginout').css("display", "");
					$('#username_li').text(data.data.LoginName);
				    //	gettouxiang();
					$("#touxiangicon").attr('src', storage.getItem("newhead"));
					$("#uploadImg").attr('src', storage.getItem("newhead"));
				} else {
					$("#touxiangicon").attr('src', storage.getItem("oldhead"));
					var str = location.href;
					if(str.indexOf("index.html") <= 0) {
						$.dialog('alert', '提示', '请先登录...', 1000, function() {
							location.href = "index.html";
						});
					}
				}
			},
			error: function() {
				$.tooltip('服务器异常！');

			},
			dataType: "json"
		})

	$('#butlogin').click(function() {
		$('#span_name').text("");
		$('#span_psw').text("");
		if($('#signin-username').val().length < 2) {
			$('#span_name').text("请输入用户名");
			return false;
		}
		if($('#signin-password').val().length < 2) {
			$('#span_psw').text("请输入密码");
			return false;
		}
		$.ajax({
		    type: 'POST',
		    url: '../../Backstage/Pages/handle/login.ashx',
		    data: {
		        "LoginName": $("#signin-username").val(),
		        "LoginPwd": $("#signin-password").val()
		    },
		    success: function (data) {
		        if (data.result == 100) {
		            $('#login_li').css("display", "none");
		            $('#register_li').css("display", "none");
		            $('#username_li').css("display", "");
		            $('#loginout').css("display", "");
		            gettouxiang();
		            //窗口消失
		            $('.cd-user-modal').removeClass('is-visible');
		            $('#username_li').text(data.data.LoginName);
		        } else {
		            alert("密码或用户名错误");
		        }
		    },
		    error: function () {
		        $.tooltip('服务器异常');
		    },
		    dataType: "json"
		});
	})

	$('#butresiger').click(function() {
		$('#respan_name').text("");
		$('#respan_phone').text("");
		$('#respan_psd').text("");
		$('#respan_repsd').text("");
		if($('#resignup-username').val().length < 2) {
			$('#respan_name').text("请输入用户名");
			return false;
		}
		if($('#resignup-email').val().length < 10) {
			$('#respan_phone').text("请输入手机号");
			return false;
		}
		if($('#resignup-password').val().length < 2) {
			$('#respan_psd').text("请输入密码");
			return false;
		}
		if($('#resignup-password2').val().length < 2) {
			$('#respan_repsd').text("请再次输入密码");
			return false;
		}
		if($('#resignup-password2').val() != $('#resignup-password').val()) {
			$('#respan_repsd').text("两次密码不一样");
			return false;
		}

		$.ajax({
			type: 'POST',
			url: "../../Backstage/Pages/handle/register.ashx",
			data: {
				"LoginName": $("#resignup-username").val(),
				"LoginPwd": $("#resignup-password").val(),
				"againPassWord": $("#resignup-password2").val(),
				"phone": $("#resignup-email").val()
			},
			success: function(data) {
				var result = data.result;
				if(result == 103) {
					$.tooltip('注册成功', 20000, true);
				} else {
					$.tooltip('注册失败');
				}
			},
			error: function() {
				$.tooltip('服务器异常');
			},
			dataType: "json"
		});
	})

	$('#loginout').click(function() {

		$.ajax({
			type: 'POST',
			url: '../../Backstage/Pages/handle/exit.ashx',
			success: function(data) {
				$('#login_li').css("display", "");
				$('#register_li').css("display", "");
				$('#username_li').css("display", "none");
				$('#loginout').css("display", "none");
				location.href = "index.html";
			},  
			error: function() {
				$.tooltip('服务器异常');
			},
			dataType: "json"
		})
	})
});